package com.time.hellotime.webserver.listener;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.time.hellotime.components.dialog.LoadingDialog;
import com.yanzhenjie.nohttp.error.NetworkError;
import com.yanzhenjie.nohttp.error.NotFoundCacheError;
import com.yanzhenjie.nohttp.error.ServerError;
import com.yanzhenjie.nohttp.error.TimeoutError;
import com.yanzhenjie.nohttp.error.URLError;
import com.yanzhenjie.nohttp.error.UnKnownHostError;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public class ResponseListener<T> implements OnResponseListener<T> {

    private Context mContext;
    private Request<T> mRequest;
    private ResponseCallback<T> mCallback;
    private Dialog mDialog;
    private boolean isShowError;

    /**
     * @param request 请求对象
     * @param context 上下文
     * @param callback 回调接口
     * @param isShowDialog 是否显示Dialog
     * @param isCanCancel Dialog是否能够取消
     * @param isShowError 是否显示错误信息
     */
    public ResponseListener(Request<T> request, Context context, ResponseCallback<T> callback,
                            boolean isShowDialog, boolean isCanCancel, boolean isShowError) {
        this.mContext = context;
        this.mRequest = request;
        this.mCallback = callback;
        this.isShowError = isShowError;
        if (context != null && isShowDialog){
            this.mDialog = new LoadingDialog(context);
            this.mDialog.setCancelable(isCanCancel);
            this.mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    mRequest.cancel();
                }
            });
        }
    }

    @Override
    public void onStart(int what) {
        if (this.mDialog != null){
            if (!this.mDialog.isShowing()){
                this.mDialog.show();
            }
        }
    }

    @Override
    public void onSucceed(int what, Response<T> response) {
        if (this.mCallback != null){
            this.mCallback.onSucceed(what, response);
        }
    }

    @Override
    public void onFailed(int what, Response<T> response) {
        Exception exception = null;
        if (isShowError) {
            exception = response.getException();
            if (exception instanceof ServerError) {// 服务器错误
                Toast.makeText(this.mContext, "服务器发生错误", Toast.LENGTH_SHORT).show();
            } else if (exception instanceof NetworkError) {// 网络不好
                Toast.makeText(this.mContext, "请检查网络设置", Toast.LENGTH_SHORT).show();
            } else if (exception instanceof TimeoutError) {// 请求超时
                Toast.makeText(this.mContext, "网络请求超时", Toast.LENGTH_SHORT).show();
            } else if (exception instanceof UnKnownHostError) {// 找不到服务器
                Toast.makeText(this.mContext, "未发现指定服务器", Toast.LENGTH_SHORT).show();
            } else if (exception instanceof URLError) {// URL是错的
                Toast.makeText(this.mContext, "请求链接错误", Toast.LENGTH_SHORT).show();
            } else if (exception instanceof NotFoundCacheError) {
                Toast.makeText(this.mContext, "没有发现缓存", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this.mContext, exception.getMessage(), Toast.LENGTH_SHORT).show();
            }
            return;
        }
        if (this.mCallback != null){
            this.mCallback.onFailed(what, response.getTag(), exception, response.getNetworkMillis());
        }
    }

    @Override
    public void onFinish(int what) {
        if (this.mDialog != null){
            if (this.mDialog.isShowing()){
                this.mDialog.dismiss();
            }
        }
    }
}
